package com.sduept.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.*;


public class CdzlbTools {
    public static byte[] getContent(String filePath) throws IOException {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            //log.info("file too big...");
            System.out.println("file too big");

            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] content = new byte[(int) fileSize];
        int offset = 0;
        int numRead;
        while (offset < content.length
                && (numRead = fi.read(content, offset, content.length - offset)) >= 0) {
            offset += numRead;
        }
        // 确保所有数据均被读取
        if (offset != content.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return content;
    }
    public static double[] asDoubleArray(byte[] input){
        if(null == input ){
            return null;
        }
        DoubleBuffer buffer = ByteBuffer.wrap(input).asDoubleBuffer();
        double[] res = new double[buffer.remaining()];
        buffer.get(res);
        return res;
    }
    public static float[] asFloatArray(byte[] input){
        if(null == input ){
            return null;
        }
        FloatBuffer buffer = ByteBuffer.wrap(input).asFloatBuffer();
        float[] res = new float[buffer.remaining()];
        buffer.get(res);
        return res;
    }
    public static int[] asIntArray(byte[] input){
        if(null == input ){
            return null;
        }
        IntBuffer buffer = ByteBuffer.wrap(input).asIntBuffer();
        int[] res = new int[buffer.remaining()];
        buffer.get(res);
        return res;
    }
    public static long[] asLongArray(byte[] input){
        if(null == input ){
            return null;
        }
        LongBuffer buffer = ByteBuffer.wrap(input).asLongBuffer();
        long[] res = new long[buffer.remaining()];
        buffer.get(res);
        return res;
    }
    public static short[] asShortArray(byte[] input){
        if(null == input ){
            return null;
        }
        ShortBuffer buffer = ByteBuffer.wrap(input).asShortBuffer();
        short[] res = new short[buffer.remaining()];
        buffer.get(res);
        return res;
    }


}
